package com.lineage.data.npc.quest;

import com.lineage.data.cmd.CreateNewItem;
import com.lineage.data.executor.NpcExecutor;
import com.lineage.server.datatables.ExpTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Npc_Marbin extends NpcExecutor {
  private static final Log _log = LogFactory.getLog(Npc_Marbin.class);
  
  public static NpcExecutor get() {
    return new Npc_Marbin();
  }
  
  public int type() {
    return 3;
  }
  
  public void talk(L1PcInstance pc, L1NpcInstance npc) {
    try {
      if (pc.getLevel() < 52) {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest8"));
        return;
      } 
      if (pc.getInventory().checkItem(92340)) {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest3"));
      } else {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest1"));
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void action(L1PcInstance pc, L1NpcInstance npc, String cmd, long amount) {
    boolean isCloseList = false;
    long needExp = ExpTable.getNeedExpNextLevel(49);
    if (cmd.equalsIgnoreCase("a")) {
      L1PcInventory inv = pc.getInventory();
      if (inv != null)
        if (inv.checkItem(92124) && inv.checkItem(92120, 100L) && inv.checkItem(92118)) {
          inv.consumeItem(92124, 1L);
          inv.consumeItem(92118, 1L);
          L1ItemInstance pcItem = pc.getInventory().findItemId(92120);
          inv.removeItem(pcItem);
          L1ItemInstance item = ItemTable.get().createItem(92125);
          if (item != null) {
            item.setCount(5L);
            item.setIdentified(true);
            if (pc.getInventory().checkAddItem(item, 5L) == 0) {
              pc.getInventory().storeItem(item);
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item.getLogName()));
              pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest2"));
            } 
            double addExp = 720000.0D;
            double exppenalty = ExpTable.getPenaltyRate(pc.getLevel());
            pc.addExp((long)(720000.0D * exppenalty));
          } 
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest4"));
        } else {
          pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest5"));
        }  
    } else if (cmd.equalsIgnoreCase("b")) {
      if (pc.getLevel() >= 52) {
        L1PcInventory inv = pc.getInventory();
        if (inv == null || inv.checkItem(92340)) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("你已经领取过袋子"));
          return;
        } 
        L1ItemInstance item2 = ItemTable.get().createItem(92340);
        if (item2 != null) {
          item2.setCount(1L);
          item2.setIdentified(true);
          if (pc.getInventory().checkAddItem(item2, 1L) == 0) {
            pc.getInventory().storeItem(item2);
            pc.sendPackets((ServerBasePacket)new S_ServerMessage(403, item2.getLogName()));
            pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest2"));
          } 
        } 
      } else {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest8"));
      } 
    } else if (cmd.equalsIgnoreCase("c")) {
      L1PcInventory inv = pc.getInventory();
      if (inv.checkItem(92119) && inv.checkItem(92120, 100L) && inv.checkItem(92124)) {
        inv.consumeItem(92119, 1L);
        inv.consumeItem(92124, 1L);
        L1ItemInstance pcItem = pc.getInventory().findItemId(92120);
        inv.removeItem(pcItem);
        needExp = (int)(needExp * 0.06D);
        pc.addExp((int)needExp);
        CreateNewItem.createNewItem_NPC(pc, npc.getName(), 92126, 1);
        pc.sendPackets((ServerBasePacket)new S_SkillSound(pc.getId(), 3944));
        pc.broadcastPacketAll((ServerBasePacket)new S_SkillSound(pc.getId(), 3944));
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest6"));
      } else {
        pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(npc.getId(), "marbinquest5"));
      } 
    } 
  }
}
